$(function() {
    $(".btn-submit-addevent").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (checkValidate('error-msg')) {
                $(".btn-submit-addevent").attr("disabled", "disabled");
                if ($(this).hasClass("add-event")) {
                    event_edit_id = 0;
                } else {
                    event_edit_id = sessionStorage.event_edit_id;
                }
                if ($('.event_end_time').val() == "End Time") {
                    $('.event_end_time').val('').prop('type', 'time');
                }
                var description = $('textarea[name="description"]').val();
                if(description=="Description"){
                    description ='';
                }
                $.post(localStorage.domain_app+"/webservice/event/updateEvent.php", {
                    event_name: $('input[name="event-name"]').val(),
                    event_date: $('input[name="event-date"]').val(),
                    start_time: $('input[name="start-time"]').val(),
                    end_time: $('input[name="end-time"]').val(),
                    location: $('input[name="location"]').val(),
                    description: description,
                    user_id: localStorage.userID,
                    event_id: event_edit_id,
                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;

                    } else {
                        print_alert(res.msg);
                    }
                    if (msgEr === '') {
                        wd_redirect("event.html");

                    } else {
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    $(".btn-submit-addevent").removeAttr("disabled");
                }, "json");
            }
        } else {
            return false;
        }
    });
    //paging
    var killScroll = false;
    $(window).scroll(function() {
        if ($('#end_paging').val() == 0) {
            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                $('.wd-loading').show();
                if (killScroll == false) {
                    killScroll = true;
                    $.post(localStorage.domain_app+"/webservice/event/index.php", {
                        page: $('#page').val(),
                        user_id: localStorage.userID
                    }, function(res) {
                        $('.wd-list-event').append(res.html);
                        $('#page').val(res.page);
                        $('#end_paging').val(res.end_paging);
                        saveEventID();
                        deleteEvent();
                        killScroll = false;
                        $('.wd-loading').hide();
                    }, "json");
                } else {
                    return false;
                }
            }
        }
    });
    $('.input_text').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");
        } else if (this.value != this.defaultValue) {
            this.select();
        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
    $('textarea').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");
        } else if (this.value != this.defaultValue) {        
            this.select();
        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });

    $('.event_start_time').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val('').prop('type', 'time');
            this.select();
        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            $(this).prop('type', 'text');
            this.value = (this.defaultValue ? this.defaultValue : '');
           

        }
    });

    $('.event_end_time').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val('').prop('type', 'time');           
            this.select();
        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            $(this).prop('type', 'text');
            this.value = (this.defaultValue ? this.defaultValue : '');            
        }
    });

    $('.event_date').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == "Date (mm/dd/yy)*") {
            $(this).val('').attr('type', 'date');
            this.select();

        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = "Date (mm/dd/yy)*";
            $(this).attr('type', 'text');

            

        }
    });
});
function checkValidate(id_append) {
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please enter %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="event-name"]').val() == '' || $('input[name="event-name"]').val() == 'Event title*') {
        msgEr = errormsg[2].replace('%s', 'event title');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="event-name"]'), num_milliseconds);
        return false;
    }

    if ($('input[name="event-date"]').val() == '' || $('input[name="event-date"]').val() == 'Date (mm/dd/yy)*') {
        msgEr = errormsg[2].replace('%s', 'event date');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="event-date"]'), num_milliseconds);
        return false;
    }
    
    //start time
    if ($('input[name="start-time"]').val() == '' || $('input[name="start-time"]').val() == 'Start time*') {
        msgEr = errormsg[2].replace('%s', 'event start time');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="start-time"]'), num_milliseconds);
        return false;
    }
   

    //location
    if ($('input[name="location"]').val() == '' || $('input[name="location"]').val() == 'Location*') {
        msgEr = errormsg[2].replace('%s', 'event location');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="location"]'), num_milliseconds);
        return false;
    }
   

    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
//            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isLengthBetween(str, min, max) {
    if (max) {
        return (str.length >= min) && (str.length <= max);
    } else {
        return (str.length >= min);
    }
}
function saveEventID() {
    if (typeof(Storage) !== "undefined")
    {
        $(".wd-edit-guest").click(function() {
            sessionStorage.event_edit_id = $(this).attr('rel');
            return true;
        });
        $(".wd-list-event-act-add").click(function() {
            sessionStorage.event_edit_id = $(this).attr('rel');
            wd_redirect("addGuestForEvent.html");
            return true;
        });
        $(".btn-event-act-adress").click(function() {
            sessionStorage.event_edit_id = $(this).attr('rel');
            wd_redirect("requestAddress.html");
            return true;
        });
        var btn_send_mail = false;
        $(".wd-list-event-act-email").click(function() {
            $('.wd-loading').show();
            if (btn_send_mail == false) {

                btn_send_mail = true;
                $.post(localStorage.domain_app+"/webservice/event/send_email_guest_list_info_by_event.php", {
                    user_id: localStorage.userID,
                    event_id: $(this).attr("rel")
                }, function(res) {
                    btn_send_mail = false;
                    print_alert(res.msg);
                     $('.wd-loading').hide();
                }, "json");
            }
        });
        $(".wd-list-event-act-invite").click(function() {
            if($(this).is('[rev]')){
                print_alert($(this).attr("rev")+"'s start time is missing. Please come back to the event screen and make changes.");
                return false;
            }
            sessionStorage.select_event_id = $(this).attr("rel");
            sessionStorage.event_edit_id =  $(this).attr("rel");
            sessionStorage.invite_guest_back="event_detail";
            wd_redirect("inviteGuests.html");
            return true;
        });
    }
    else
    {
        return false;
    }


}
function actionDeleteEvent(button,event_id) {
    if (button == 1) {
        
        var user_id = 0;
        if (typeof(Storage) !== "undefined")
        {
            user_id = localStorage.userID;
        }
        $.post(localStorage.domain_app+"/webservice/event/deleteEvent.php", {
            event_id: event_id,
            user_id: user_id
        }, function(res) {
            btn_delete = false;
            var num_milliseconds = 3000;
            if (res.error == 1) {
                $('#error-msg').addClass('active').children().html(res.msg);
                hideError_2('error-msg', num_milliseconds);

            } else {
                wd_redirect("event.html");
            }
        }, "json");
        
    } else {
        btn_delete = false;
        return false;
    }
}
var btn_delete = false;
function deleteEvent() {
    //delete guest
    
    $(".wd-delete-guest").click(function() {

        if (btn_delete === false) {
            btn_delete = true;
            var event_id = $(this).attr('rel');
            
            navigator.notification.confirm(
                "Are you sure you want to delete this event?",
                function(buttonIndex){
                    actionDeleteEvent(buttonIndex,event_id);
                }
                , // callback to invoke with index of button pressed
                'Wedding Helper', // title
                'OK,Cancel'          // buttonLabels
                );
            
            
        }
        

    });
}

